package com.bonc.modules.rpt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonc.datasource.annotation.DataSource;
import com.bonc.modules.common.entity.Region;
import com.bonc.modules.common.service.CommonService;
import com.bonc.modules.rpt.dao.ServSatisSurveyItemDao;
import com.bonc.modules.rpt.entity.ServSatisSurveyItem;
import com.bonc.modules.rpt.param.SurveyParam;
import com.bonc.modules.rpt.result.SurveyResult;
import com.bonc.modules.rpt.result.SurveyResultY;
import com.bonc.modules.rpt.service.ServSatisSurveyItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

@DataSource("gbasedata")
@Service
public class ServSatisSurveyItemServiceImpl extends ServiceImpl<ServSatisSurveyItemDao, ServSatisSurveyItem> implements ServSatisSurveyItemService {

    private static char[] cnArr = new char[]{'一', '二', '三', '四', '五', '六', '七', '八', '九'};

    @Autowired
    private ServSatisSurveyItemDao servSatisSurveyItemDao;

    @Autowired
    private CommonService commonService;


    private List<Integer> getRecentMonth(Integer endDate, int num){
        LocalDateTime localDateTime = LocalDateTime.of(endDate.intValue() / 100, endDate.intValue() % 100, 1, 0, 0);
        List<Integer> dateList = new ArrayList<Integer>();
        for(int i = 0; i < num; i++){
            dateList.add(localDateTime.getYear() * 100 + localDateTime.getMonthValue());
            localDateTime = localDateTime.minus( 1, ChronoUnit.MONTHS);
        }
        return dateList;
    }

    private List<Integer> getRecentSeason(Integer endDate, int num){
        LocalDateTime localDateTime = LocalDateTime.of(endDate.intValue() / 100, endDate.intValue() % 100, 1, 0, 0);
        List<Integer> dateList = new ArrayList<Integer>();
        for(int i = 0; i < num; i++){
            dateList.add(localDateTime.getYear() * 100 + localDateTime.getMonthValue());
            localDateTime = localDateTime.minus( 3, ChronoUnit.MONTHS);
        }
        return dateList;
    }

    private void convertParam(SurveyParam servSatisSurveyItem){
        if(servSatisSurveyItem.getGroupCode() != null && !servSatisSurveyItem.equals("")){
            String[] paramArr = servSatisSurveyItem.getGroupCode().split(":");
            if(paramArr.length >= 1 && paramArr[0] != null){
                servSatisSurveyItem.setItemCode(paramArr[0]);
            }
            if(paramArr.length > 1 && paramArr[1] != null){
                servSatisSurveyItem.setGroupCode(paramArr[1]);
            }
        }
    }

    @Override
    public SurveyResult getServSatisSurveyItem(SurveyParam surveyParam)
    {
        convertParam(surveyParam);
        LambdaQueryWrapper<ServSatisSurveyItem> queryWrapper = new LambdaQueryWrapper<>();
        if(surveyParam.getRegionCode() != null && !surveyParam.getRegionCode().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getRegionCode, surveyParam.getRegionCode());
        }
        if(surveyParam.getCityCode() != null && !surveyParam.getCityCode().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getCityCode, surveyParam.getCityCode());
        }
        if(surveyParam.getDealDate() != null){
            queryWrapper.eq(ServSatisSurveyItem::getDealDate, surveyParam.getDealDate());
        }
        if(surveyParam.getDataType() != null && !surveyParam.getDataType().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getDataType, surveyParam.getDataType());
        }
        if(surveyParam.getItemCode() != null && !surveyParam.getItemCode().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getItemCode, surveyParam.getItemCode());
        }
        if(surveyParam.getGroupCode() != null && !surveyParam.getGroupCode().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getGroupCode, surveyParam.getGroupCode());
        }
        List<ServSatisSurveyItem> servSatisSurveyItemList = this.baseMapper.selectList(queryWrapper);
        SurveyResult surveyResult = new SurveyResult();
        if(servSatisSurveyItemList == null || servSatisSurveyItemList.size() == 0)
        {
            return surveyResult;
        }
        surveyResult.setData(servSatisSurveyItemList.get(0));
        return surveyResult;
    }

    private void generateTrendXAxis(SurveyResult surveyResult, String dataType, List<Integer> dateList)
    {
        for (int i = dateList.size() - 1; i >= 0; i--) {
            String dealDateStr = dateList.get(i).toString();
            if("J".equals(dataType)){
                int mon = Integer.valueOf(dealDateStr.substring(4));
                surveyResult.getXAxis().add(dealDateStr.substring(2, 4) + "年第" + cnArr[mon / 3 - 1] + "季度");
            }
            if("M".equals(dataType)){
                int mon = Integer.valueOf(dealDateStr.substring(4));
                surveyResult.getXAxis().add(dealDateStr.substring(0, 4) + "-" + dealDateStr.substring(4));
            }
        }
    }

    private void generateLeadingValueResult(SurveyResult surveyResult, List<ServSatisSurveyItem> servSatisSurveyItemList, Boolean generateIncrease){
        SurveyResultY surveyResultY = new SurveyResultY();
        SurveyResultY surveyResultYIncreaseLeading = new SurveyResultY();
        for (ServSatisSurveyItem servSatisSurveyItem : servSatisSurveyItemList) {
            if(servSatisSurveyItem.getLeadingValue() != null) {
                surveyResultY.getData().add(servSatisSurveyItem.getLeadingValue().toString());
            } else {
                surveyResultY.getData().add("");
            }
            if(generateIncrease) {
                if(servSatisSurveyItem.getIncreaseLeadingValue() != null) {
                    surveyResultYIncreaseLeading.getData().add(servSatisSurveyItem.getIncreaseLeadingValue().toString());
                } else {
                    surveyResultYIncreaseLeading.getData().add("");
                }
            }
        }
        surveyResultY.setLegend("领先值");
        surveyResult.getYAxis().add(surveyResultY);

        if(generateIncrease) {
            surveyResultYIncreaseLeading.setLegend("环比");
            surveyResult.getYAxis().add(surveyResultYIncreaseLeading);
        }
    }

    private void generateQuarterValueResult(SurveyResult surveyResult, List<ServSatisSurveyItem> servSatisSurveyItemList){
        SurveyResultY ydSurveyResultY = new SurveyResultY();
        SurveyResultY ltSurveyResultY = new SurveyResultY();
        SurveyResultY dxSurveyResultY = new SurveyResultY();
        for (ServSatisSurveyItem servSatisSurveyItem : servSatisSurveyItemList) {
            ydSurveyResultY.getData().add(servSatisSurveyItem.getYdQuarterValue() == null ? "" : servSatisSurveyItem.getYdQuarterValue().toString());
            ltSurveyResultY.getData().add(servSatisSurveyItem.getLtQuarterValue() == null ? "" : servSatisSurveyItem.getLtQuarterValue().toString());
            dxSurveyResultY.getData().add(servSatisSurveyItem.getDxQuarterValue() == null ? "" : servSatisSurveyItem.getDxQuarterValue().toString());
        }
        ydSurveyResultY.setLegend("移动");
        ltSurveyResultY.setLegend("联通");
        dxSurveyResultY.setLegend("电信");
        surveyResult.getYAxis().add(ydSurveyResultY);
        surveyResult.getYAxis().add(ltSurveyResultY);
        surveyResult.getYAxis().add(dxSurveyResultY);
    }

    private void generateLeadingTrendValueResult(SurveyResult surveyResult, List<ServSatisSurveyItem> servSatisSurveyItemList, Boolean generateIncrease, List<Integer> dateList){
        Map<Long, ServSatisSurveyItem> dataMap = null;
        if(servSatisSurveyItemList != null){
            dataMap = servSatisSurveyItemList.stream().collect(Collectors.toMap(servSatisSurveyItem -> servSatisSurveyItem.getDealDate(), servSatisSurveyItem -> servSatisSurveyItem));
        } else {
            dataMap = new HashMap<>();
        }
        SurveyResultY surveyResultY = new SurveyResultY();
        SurveyResultY surveyResultYIncreaseLeading = new SurveyResultY();

        for (int i = dateList.size() - 1; i >= 0; i--) {
            Integer date = dateList.get(i);
            ServSatisSurveyItem servSatisSurveyItem = dataMap.get(Long.valueOf(date.toString()));
            if(servSatisSurveyItem != null && servSatisSurveyItem.getLeadingValue() != null){
                surveyResultY.getData().add(servSatisSurveyItem.getLeadingValue().toString());
            } else {
                surveyResultY.getData().add("");
            }
            if(generateIncrease) {
                if(servSatisSurveyItem != null && servSatisSurveyItem.getIncreaseLeadingValue() != null) {
                    surveyResultYIncreaseLeading.getData().add(servSatisSurveyItem.getIncreaseLeadingValue().toString());
                } else {
                    surveyResultYIncreaseLeading.getData().add("");
                }
            }
        }

        surveyResultY.setLegend("领先值");
        surveyResult.getYAxis().add(surveyResultY);

        if(generateIncrease) {
            surveyResultYIncreaseLeading.setLegend("环比");
            surveyResult.getYAxis().add(surveyResultYIncreaseLeading);
        }
    }

    private void generateQuarterTrendValueResult(SurveyResult surveyResult, List<ServSatisSurveyItem> servSatisSurveyItemList, List<Integer> dateList){

        Map<Long, Double> dataMapYd = new HashMap<Long, Double>();
        Map<Long, Double> dataMapLt = new HashMap<Long, Double>();
        Map<Long, Double> dataMapDx = new HashMap<Long, Double>();
        if(servSatisSurveyItemList != null){
            for(ServSatisSurveyItem servSatisSurveyItem : servSatisSurveyItemList){
                dataMapYd.put(servSatisSurveyItem.getDealDate(), servSatisSurveyItem.getYdQuarterValue());
                dataMapLt.put(servSatisSurveyItem.getDealDate(), servSatisSurveyItem.getLtQuarterValue());
                dataMapDx.put(servSatisSurveyItem.getDealDate(), servSatisSurveyItem.getDxQuarterValue());
            }
        }
        SurveyResultY ydSurveyResultY = new SurveyResultY();
        SurveyResultY ltSurveyResultY = new SurveyResultY();
        SurveyResultY dxSurveyResultY = new SurveyResultY();
        for (int i = dateList.size() - 1; i >= 0; i--) {
            Integer date = dateList.get(i);
            ydSurveyResultY.getData().add((dataMapYd.get(Long.valueOf(date.toString())) == null ? "" : dataMapYd.get(Long.valueOf(date.toString()))).toString());
            ltSurveyResultY.getData().add((dataMapLt.get(Long.valueOf(date.toString())) == null ? "" : dataMapLt.get(Long.valueOf(date.toString()))).toString());
            dxSurveyResultY.getData().add((dataMapDx.get(Long.valueOf(date.toString())) == null ? "" : dataMapDx.get(Long.valueOf(date.toString()))).toString());
        }
        ydSurveyResultY.setLegend("移动");
        ltSurveyResultY.setLegend("联通");
        dxSurveyResultY.setLegend("电信");
        surveyResult.getYAxis().add(ydSurveyResultY);
        surveyResult.getYAxis().add(ltSurveyResultY);
        surveyResult.getYAxis().add(dxSurveyResultY);
    }

    private LambdaQueryWrapper<ServSatisSurveyItem> generateTrendLambdaQueryWrapper(SurveyParam surveyParam, List<Integer> dateList)
    {
        LambdaQueryWrapper<ServSatisSurveyItem> queryWrapper = new LambdaQueryWrapper<>();
        if(surveyParam.getRegionCode() != null && !surveyParam.getRegionCode().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getRegionCode, surveyParam.getRegionCode());
        }
        if(surveyParam.getCityCode() != null && !surveyParam.getCityCode().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getCityCode, surveyParam.getCityCode());
        }
        if(surveyParam.getItemCode() != null && !surveyParam.getItemCode().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getItemCode, surveyParam.getItemCode());
        }
        if(surveyParam.getGroupCode() != null && !surveyParam.getGroupCode().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getGroupCode, surveyParam.getGroupCode());
        }
        if(surveyParam.getDataType() != null && !surveyParam.getDataType().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getDataType, surveyParam.getDataType());
        }
        if(surveyParam.getDataType() != null && surveyParam.getDealDate() != null){
            queryWrapper.in(ServSatisSurveyItem::getDealDate, dateList);
        }

        queryWrapper.orderByDesc(ServSatisSurveyItem::getDealDate);
        return queryWrapper;
    }

    @Override
    public SurveyResult getLeadingTrend(SurveyParam surveyParam){
        convertParam(surveyParam);
        List<Integer> dateList = null;
        if(surveyParam.getDataType() != null && surveyParam.getDealDate() != null){
            if(surveyParam.getDataType().equals("J")){
                dateList = getRecentSeason(surveyParam.getDealDate(), 4);
            } else if(surveyParam.getDataType().equals("M")){
                dateList = getRecentMonth(surveyParam.getDealDate(), 12);
            }
        }
        LambdaQueryWrapper<ServSatisSurveyItem> queryWrapper = generateTrendLambdaQueryWrapper(surveyParam, dateList);
        List<ServSatisSurveyItem> servSatisSurveyItemList = this.baseMapper.selectList(queryWrapper);
        SurveyResult surveyResult = new SurveyResult();
        generateTrendXAxis(surveyResult, surveyParam.getDataType(), dateList);
        generateLeadingTrendValueResult(surveyResult, servSatisSurveyItemList, true, dateList);

        return surveyResult;
    }

    @Override
    public SurveyResult getQuarterTrend(SurveyParam surveyParam){
        convertParam(surveyParam);
        List<Integer> dateList = null;
        if(surveyParam.getDataType() != null && surveyParam.getDealDate() != null){
            if(surveyParam.getDataType().equals("J")){
                dateList = getRecentSeason(surveyParam.getDealDate(), 4);
            } else if(surveyParam.getDataType().equals("M")){
                dateList = getRecentMonth(surveyParam.getDealDate(), 12);
            }
        }
        LambdaQueryWrapper<ServSatisSurveyItem> queryWrapper = generateTrendLambdaQueryWrapper(surveyParam, dateList);
        List<ServSatisSurveyItem> servSatisSurveyItemList = this.baseMapper.selectList(queryWrapper);
        SurveyResult surveyResult = new SurveyResult();
        generateTrendXAxis(surveyResult, surveyParam.getDataType(), dateList);
        generateQuarterTrendValueResult(surveyResult, servSatisSurveyItemList, dateList);

        return surveyResult;
    }


    private LambdaQueryWrapper<ServSatisSurveyItem> generateSatisByRegionLambdaQueryWrapper(SurveyParam surveyParam) {
        LambdaQueryWrapper<ServSatisSurveyItem> queryWrapper = new LambdaQueryWrapper<>();
        if(surveyParam.getRegionCode() != null){
            if(surveyParam.getRegionCode().equals("00")) {
                queryWrapper.ne(ServSatisSurveyItem::getRegionCode, surveyParam.getRegionCode());
                queryWrapper.eq(ServSatisSurveyItem::getCityCode, "00");
            } else {
                queryWrapper.eq(ServSatisSurveyItem::getRegionCode, surveyParam.getRegionCode());
                queryWrapper.ne(ServSatisSurveyItem::getCityCode, "00");
            }
        }
        if(surveyParam.getItemCode() != null && !surveyParam.getItemCode().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getItemCode, surveyParam.getItemCode());
        }
        if(surveyParam.getGroupCode() != null && !surveyParam.getGroupCode().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getGroupCode, surveyParam.getGroupCode());
        }
        if(surveyParam.getDealDate() != null){
            queryWrapper.eq(ServSatisSurveyItem::getDealDate, surveyParam.getDealDate());
        }
        if(surveyParam.getDataType() != null && !surveyParam.getDataType().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getDataType, surveyParam.getDataType());
        }
        queryWrapper.orderByAsc(ServSatisSurveyItem::getRegionCode, ServSatisSurveyItem::getCityCode);

        return queryWrapper;
    }

    private void generateSatisByRegionXAxis(SurveyResult surveyResult, List<ServSatisSurveyItem> servSatisSurveyItemList, SurveyParam surveyParam){
        List<Region> list = null;
        if(surveyParam.getRegionCode() != null) {
            if (surveyParam.getRegionCode().equals("00")) {
                Region region = new Region();
                region.setCityCode("00");
                list = commonService.getCity(region);
            } else {
                Region region = new Region();
                region.setCityCode(surveyParam.getRegionCode());
                region.setCountyCode("00");
                list = commonService.getCounty(region);
            }
        }

        for (ServSatisSurveyItem servSatisSurveyItem : servSatisSurveyItemList) {
            String xName = "";
            if(surveyParam.getRegionCode() != null) {
                if (surveyParam.getRegionCode().equals("00")) {
                    xName = servSatisSurveyItem.getRegionCode();
                    for(Region region : list){
                        if(region.getCityCode().equals(servSatisSurveyItem.getRegionCode())){
                            xName = region.getCityName();
                            break;
                        }
                    }
                } else {
                    xName = servSatisSurveyItem.getCityCode();
                    for(Region region : list){
                        if(region.getCountyCode().equals(servSatisSurveyItem.getCityCode())){
                            xName = region.getCountyName();
                            break;
                        }
                    }
                }
            }
            surveyResult.getXAxis().add(xName);
        }
    }

    @Override
    public SurveyResult getLeadingSatisByRegion(SurveyParam surveyParam)
    {
        convertParam(surveyParam);
        LambdaQueryWrapper<ServSatisSurveyItem> queryWrapper = generateSatisByRegionLambdaQueryWrapper(surveyParam);
        List<ServSatisSurveyItem> servSatisSurveyItemList = this.baseMapper.selectList(queryWrapper);
        SurveyResult surveyResult = new SurveyResult();
        generateSatisByRegionXAxis(surveyResult, servSatisSurveyItemList, surveyParam);

        generateLeadingValueResult(surveyResult, servSatisSurveyItemList, true);
        return surveyResult;
    }

    @Override
    public SurveyResult getQuarterSatisByRegion(SurveyParam surveyParam)
    {
        convertParam(surveyParam);
        LambdaQueryWrapper<ServSatisSurveyItem> queryWrapper = generateSatisByRegionLambdaQueryWrapper(surveyParam);
        List<ServSatisSurveyItem> servSatisSurveyItemList = this.baseMapper.selectList(queryWrapper);
        SurveyResult surveyResult = new SurveyResult();
        generateSatisByRegionXAxis(surveyResult, servSatisSurveyItemList, surveyParam);

        generateQuarterValueResult(surveyResult, servSatisSurveyItemList);
        return surveyResult;
    }

    private LambdaQueryWrapper<ServSatisSurveyItem> generateSatisFactorsLambdaQueryWrapper(SurveyParam surveyParam) {
        LambdaQueryWrapper<ServSatisSurveyItem> queryWrapper = new LambdaQueryWrapper<>();
        if(surveyParam.getRegionCode() != null && !surveyParam.getRegionCode().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getRegionCode, surveyParam.getRegionCode());
        }
        if(surveyParam.getCityCode() != null && !surveyParam.getCityCode().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getCityCode, surveyParam.getCityCode());
        }
        if(surveyParam.getDealDate() != null){
            queryWrapper.eq(ServSatisSurveyItem::getDealDate, surveyParam.getDealDate());
        }
        if(surveyParam.getDataType() != null && !surveyParam.getDataType().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getDataType, surveyParam.getDataType());
        }
        if(surveyParam.getItemCode() != null && !surveyParam.getItemCode().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getItemCode, surveyParam.getItemCode());
        }
        if(surveyParam.getGroupCode() != null && !surveyParam.getGroupCode().equals("")){
            queryWrapper.in(ServSatisSurveyItem::getGroupCode, Arrays.asList(surveyParam.getGroupCode().split(",")));
        }
        queryWrapper.orderByAsc(ServSatisSurveyItem::getGroupCode);

        return queryWrapper;
    }

    @Override
    public SurveyResult getLeadingSatisFactors(SurveyParam surveyParam){
        convertParam(surveyParam);
        LambdaQueryWrapper<ServSatisSurveyItem> queryWrapper = generateSatisFactorsLambdaQueryWrapper(surveyParam);
        List<ServSatisSurveyItem> servSatisSurveyItemList = this.baseMapper.selectList(queryWrapper);
        SurveyResult surveyResult = new SurveyResult();
        for (ServSatisSurveyItem servSatisSurveyItem : servSatisSurveyItemList) {
            surveyResult.getXAxis().add(servSatisSurveyItem.getGroupName());
        }
        generateLeadingValueResult(surveyResult, servSatisSurveyItemList, false);
        return surveyResult;
    }

    @Override
    public SurveyResult getQuarterSatisFactors(SurveyParam surveyParam){
        convertParam(surveyParam);
        LambdaQueryWrapper<ServSatisSurveyItem> queryWrapper = generateSatisFactorsLambdaQueryWrapper(surveyParam);
        List<ServSatisSurveyItem> servSatisSurveyItemList = this.baseMapper.selectList(queryWrapper);
        SurveyResult surveyResult = new SurveyResult();
        for (ServSatisSurveyItem servSatisSurveyItem : servSatisSurveyItemList) {
            surveyResult.getXAxis().add(servSatisSurveyItem.getGroupName());
        }
        generateQuarterValueResult(surveyResult, servSatisSurveyItemList);
        return surveyResult;
    }

    private void generateGovtEntSatisFactorsXAxis(SurveyResult surveyResult, List<List<ServSatisSurveyItem>> servSatisSurveyItemsList) {
        if(servSatisSurveyItemsList != null && servSatisSurveyItemsList.size() > 0){
            for(int i = 0; i < servSatisSurveyItemsList.get(0).size(); i++){
                ServSatisSurveyItem servSatisSurveyItem = servSatisSurveyItemsList.get(0).get(i);
                if(servSatisSurveyItem == null || servSatisSurveyItem.getGroupName() == null || servSatisSurveyItem.getGroupName().equals("")) {
                    for(List<ServSatisSurveyItem> servSatisSurveyItemList : servSatisSurveyItemsList){
                        if(servSatisSurveyItemList.get(i) != null
                                && servSatisSurveyItemList.get(i).getGroupName() != null
                                && !servSatisSurveyItemList.get(i).getGroupName().equals("")){
                            surveyResult.getXAxis().add(servSatisSurveyItemList.get(i).getGroupName());
                            break;
                        }
                    }
                    if(surveyResult.getXAxis().size() == i) {
                        surveyResult.getXAxis().add("");
                    }
                } else {
                    surveyResult.getXAxis().add(servSatisSurveyItem.getGroupName());
                }
            }
        }

    }

    private void generateQuarterGovtEntSatisFactorsValueResult(SurveyResult surveyResult, List<List<ServSatisSurveyItem>> servSatisSurveyItemsList) {
        for(List<ServSatisSurveyItem> servSatisSurveyItemList : servSatisSurveyItemsList) {
            SurveyResultY surveyResultY = new SurveyResultY();
            for(ServSatisSurveyItem servSatisSurveyItem : servSatisSurveyItemList) {
                if(servSatisSurveyItem == null) {
                    surveyResultY.getData().add("0");
                    continue;
                }
                if(surveyResultY.getLegend() == null){
                    surveyResultY.setLegend(servSatisSurveyItem.getItemName());
                }
                if(servSatisSurveyItem.getYdQuarterValue() != null) {
                    surveyResultY.getData().add(servSatisSurveyItem.getYdQuarterValue().toString());
                } else {
                    surveyResultY.getData().add("0");
                }
            }
            surveyResult.getYAxis().add(surveyResultY);
        }
    }

    @Override
    public SurveyResult getGovtEntSatisFactors(SurveyParam surveyParam){

        String[] groupCodesArr = null;
        if(surveyParam.getGroupCode() != null) {
            groupCodesArr = surveyParam.getGroupCode().split(";");
        }
        SurveyResult surveyResult = new SurveyResult();
        if(groupCodesArr != null) {
            surveyParam.setGroupCode(null);
            List<List<ServSatisSurveyItem>> servSatisSurveyItemsList = new ArrayList<List<ServSatisSurveyItem>>();
            for(String groupCodes : groupCodesArr){
                surveyParam.setGroupCode(groupCodes);
                convertParam(surveyParam);
                if(surveyParam.getGroupCode() != null && !surveyParam.getGroupCode().equals("")){
                    ServSatisSurveyItem servSatisSurveyItem = new ServSatisSurveyItem();
                    surveyParam.setGroupCodeList(Arrays.asList(surveyParam.getGroupCode().split(",")));
                    servSatisSurveyItem.setGroupCodeList(surveyParam.getGroupCodeList());
                    servSatisSurveyItem.setItemCode(surveyParam.getItemCode());
                    servSatisSurveyItem.setRegionCode(surveyParam.getRegionCode());
                    servSatisSurveyItem.setCityCode(surveyParam.getCityCode());
                    servSatisSurveyItem.setDealDate(Long.valueOf(surveyParam.getDealDate()));
                    servSatisSurveyItem.setDataType(surveyParam.getDataType());

                    List<ServSatisSurveyItem> servSatisSurveyItemListTmp = servSatisSurveyItemDao.selectGovtEntSatisFactors(servSatisSurveyItem);
                    List<ServSatisSurveyItem> servSatisSurveyItemList = new ArrayList<ServSatisSurveyItem>();
                    int i = 0;
                    for(String groupCode : surveyParam.getGroupCodeList()){
                        for(i = 0; i < servSatisSurveyItemListTmp.size(); i++){
                            if(groupCode.equals(servSatisSurveyItemListTmp.get(i).getGroupCode())){
                                servSatisSurveyItemList.add(servSatisSurveyItemListTmp.get(i));
                                break;
                            }
                        }
                        if(i >= servSatisSurveyItemListTmp.size()){
                            servSatisSurveyItemList.add(new ServSatisSurveyItem());
                        }
                    }
                    servSatisSurveyItemsList.add(servSatisSurveyItemList);
                }
            }
            generateGovtEntSatisFactorsXAxis(surveyResult, servSatisSurveyItemsList);
            generateQuarterGovtEntSatisFactorsValueResult(surveyResult, servSatisSurveyItemsList);
        }
        return surveyResult;
    }

    private LambdaQueryWrapper<ServSatisSurveyItem> generateSatisTopLambdaQueryWrapper(SurveyParam surveyParam, Boolean isAsc){
        LambdaQueryWrapper<ServSatisSurveyItem> queryWrapper = new LambdaQueryWrapper<>();
        if(surveyParam.getRegionCode() != null){
            if(surveyParam.getRegionCode().equals("00")) {
                queryWrapper.ne(ServSatisSurveyItem::getRegionCode, surveyParam.getRegionCode());
            } else {
                queryWrapper.eq(ServSatisSurveyItem::getRegionCode, surveyParam.getRegionCode());
            }
        }
        queryWrapper.ne(ServSatisSurveyItem::getCityCode, "00");
        if(surveyParam.getDealDate() != null){
            queryWrapper.eq(ServSatisSurveyItem::getDealDate, surveyParam.getDealDate());
        }
        if(surveyParam.getDataType() != null && !surveyParam.getDataType().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getDataType, surveyParam.getDataType());
        }
        if(surveyParam.getItemCode() != null && !surveyParam.getItemCode().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getItemCode, surveyParam.getItemCode());
        }
        if(surveyParam.getGroupCode() != null && !surveyParam.getGroupCode().equals("")){
            queryWrapper.eq(ServSatisSurveyItem::getGroupCode, surveyParam.getGroupCode());
        }
        if(isAsc) {
            queryWrapper.orderByAsc(ServSatisSurveyItem::getLeadingRank);
        } else {
            queryWrapper.orderByDesc(ServSatisSurveyItem::getLeadingRank);
        }
        queryWrapper.last("limit 10");
        return queryWrapper;
    }

    private void generateSatisTopXAxis(SurveyResult surveyResult, List<ServSatisSurveyItem> servSatisSurveyItemList){
        Region region = new Region();
        for (ServSatisSurveyItem servSatisSurveyItem : servSatisSurveyItemList) {
            region.setCityCode(servSatisSurveyItem.getRegionCode());
            region.setCountyCode(servSatisSurveyItem.getCityCode());
            List<Region> list = commonService.getCounty(region);
            if(list != null && list.size() > 0){
                surveyResult.getXAxis().add(list.get(0).getCountyName());
            } else {
                surveyResult.getXAxis().add(servSatisSurveyItem.getCityCode());
            }
        }
    }

    @Override
    public SurveyResult getSatisAscTop10(SurveyParam surveyParam){
        convertParam(surveyParam);
        LambdaQueryWrapper<ServSatisSurveyItem> queryWrapper = generateSatisTopLambdaQueryWrapper(surveyParam, true);
        List<ServSatisSurveyItem> servSatisSurveyItemList = this.baseMapper.selectList(queryWrapper);
        SurveyResult surveyResult = new SurveyResult();
        generateSatisTopXAxis(surveyResult, servSatisSurveyItemList);
        generateLeadingValueResult(surveyResult, servSatisSurveyItemList, false);
        return surveyResult;
    }

    @Override
    public SurveyResult getSatisDescTop10(SurveyParam surveyParam){
        convertParam(surveyParam);
        LambdaQueryWrapper<ServSatisSurveyItem> queryWrapper = generateSatisTopLambdaQueryWrapper(surveyParam, false);
        List<ServSatisSurveyItem> servSatisSurveyItemList = this.baseMapper.selectList(queryWrapper);
        SurveyResult surveyResult = new SurveyResult();
        generateSatisTopXAxis(surveyResult, servSatisSurveyItemList);
        generateLeadingValueResult(surveyResult, servSatisSurveyItemList, false);
        return surveyResult;
    }
}
